package com.dk.academic.common;

import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.net.InetAddress;

import static com.dk.academic.common.Common.HOST;
import static com.dk.academic.common.Common.PORT;

/**
 * @Author: 廉雪峰
 * @Date: 2018/10/28 17:32
 * @Version 1.0
 */
@Component
public class ESRunner implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("执行搜索前获得连接");
        //创建客户端
        // PreBuiltTransportClient 需要版本5.+，大概是6版本以上setting不能为空 需要制定type
        Common.client = new PreBuiltTransportClient(Settings.EMPTY).addTransportAddresses(
                new InetSocketTransportAddress(InetAddress.getByName(HOST),PORT));

        System.out.println("Elasticsearch connect info:" + Common.client.toString());
    }
}
